import 'package:flutter/material.dart';

class Trippage extends StatefulWidget {
  const Trippage({super.key});

  @override
  State<Trippage> createState() => _TrippageState();
}

class _TrippageState extends State<Trippage>
    with SingleTickerProviderStateMixin {
  // 移除late，改为可空类型，并在initState中初始化
  TabController? _tabController;

  final List<String> _tabs = ["发现", "玩乐", "酒店", "美食", "亲子", "网红", "拍照技巧"];

  @override
  void initState() {
    super.initState();
    // 确保TabController一定被初始化
    _tabController = TabController(length: _tabs.length, vsync: this);
  }

  @override
  void dispose() {
    _tabController?.dispose(); // 空安全校验：避免控制器为null时调用dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: TabBar(
          controller: _tabController!, // 非空断言（因为initState中已初始化）
          tabs: _tabs.map((tab) => Tab(text: tab)).toList(),
          labelColor: Colors.orange,
          unselectedLabelColor: Colors.grey,
          tabAlignment: TabAlignment.start,
          labelStyle: const TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.w500,
          ),
          unselectedLabelStyle: const TextStyle(fontSize: 14),
          isScrollable: true,
          indicator: const UnderlineTabIndicator(
            borderSide: BorderSide(width: 2.0, color: Colors.blue),
          ),
          indicatorSize: TabBarIndicatorSize.tab,
        ),
      ),
      body: TabBarView(
        controller: _tabController!, // 非空断言
        children: _tabs.map((tab) {
          return Center(child: Text("$tab 页面内容"));
        }).toList(),
      ),
    );
  }
}
